<?php

namespace Admin\Controller;

use Admin\Logic\MemberLogic;
use Admin\Logic\TableLogic;
use Admin\Logic\UserLogic;

class TableController extends CommonController
{
    /**
     * 餐桌列表展示
     * @return [type] [description]
     */
    public function index()
    {
        // 获取所有餐桌
        $data = TableLogic::getAll();


        $this->assign('data', $data);
        $this->display();
    }



    /**
     * 添加餐桌视图
     */
    public function add()
    {
        $this->display();
    }



    /**
     * 处理餐桌逻辑
     * @return [type] [description]
     */
    public function store()
    {
        $this->checkPost();

        $param = [
            'table_name' => I('post.table_name', '', 'htmlspecialchars'),
            'is_show' => I('post.is_show')=='on'?'显示':'不显示',
        ];

        // 验证餐桌名是否已经存在
        $res = TableLogic::tableNameIsExist($param['table_name']);

        if($res['code'] == 1){
            return $this->ajaxError($res['msg']);
        } elseif($res == false) {
            return $this->ajaxError();
        }


        $user = TableLogic::create($param);

        if ($user['code'] == 1) {
            return $this->ajaxError($user['msg']);
        } else {
            return $this->ajaxSuccess();
        }
    }




    /**
     * 编辑餐桌信息
     * @return [type] [description]
     */
    public function edit()
    {

        $id = I('get.id', 0, 'intval');

        if ($id < 1) {
            E('非法操作');
        }

        $table = D('Table')->where(['id' => $id])->find();

        $this->assign('table', $table);
        $this->display();
    }




    /**
     * 处理餐桌信息
     * @return [type] [description]
     */
    public function update()
    {
        $this->checkPost();
        $param = [
            'id' => I('post.id', 0, 'intval'),
            'table_name' => I('post.table_name', '', 'htmlspecialchars'),
            'is_show' => I('post.is_show')=='on'?'显示':'不显示',

        ];

        // 验证餐桌名是否已经存在
        $res = TableLogic::tableNameIsExist($param['table_name'],$param['id']);

        if($res['code'] == 1){
            return $this->ajaxError($res['msg']);
        } elseif($res == false) {
            return $this->ajaxError();
        }


        $update = TableLogic::update($param);

        if ($update == false) {
           return $this->ajaxError('服务器找不到啦~^_^');
        }

        return $this->ajaxSuccess();
        
    }





    /**
     * 删除餐桌
     * @return [type] [description]
     */
    public function delete()
    {
        $id = I('get.id', 0, 'intval');

        $data = TableLogic::delete($id);
        if ($data > 0) {
            return $this->ajaxSuccess();
        } else {

            return $this->ajaxError('删除失败，请重试');
        }
    }






}